package com.aboo.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.aboo.po.FrameConstantPO;
import com.aboo.po.Page;
import com.aboo.pub.constant.ConfigXML;
import com.aboo.pub.exception.BasicException;
import com.aboo.service.FrameConstantService;
import com.aboo.util.RequestUtil;

public class FrameConstantAction extends BaseAction {

	private static final long serialVersionUID = -6814928017962123628L;
	private FrameConstantService frameConstantService;
	private FrameConstantPO frameConstant = new FrameConstantPO();
	
	public String list() throws BasicException {
		Map<String, String> constantMap = ConfigXML.getConstantMap();
		RequestUtil.setAttribute(request, "constantMap", constantMap);
		return BaseAction.LIST;
	}
	
	public String modify() throws BasicException {
		Integer currentPageNO = RequestUtil.getParameterIntegerDefault(request, Page.CURRENT_PAGE_NO, Page.DEFAULT_CURRENT_PAGE_NO);
		Integer pageSize = RequestUtil.getParameterIntegerDefault(request, Page.PAGE_SIZE, new Integer(5));
		Integer sectionSize = RequestUtil.getParameterIntegerDefault(request, Page.SECTION_SIZE, Page.DEFAULT_SECTION_SIZE);
		page = new Page(currentPageNO, pageSize, sectionSize);
		
		String constantName_find = RequestUtil.getParameterStringDefault(request, "constantName_find", "");
		String constantKey = RequestUtil.getParameterString(request, "constantKey");
		String constantId_modify = RequestUtil.getParameterString(request, "constantId_modify");
		
		frameConstant = new FrameConstantPO();
		frameConstant.setModuleName(constantKey);
		frameConstant.setConstantName(constantName_find);
		frameConstantService.findFrameConstantList(ri, frameConstant, page);
		List<FrameConstantPO> constantList = (List<FrameConstantPO>)ri.getObj();
		
		if (!StringUtils.isEmpty(constantId_modify)) {
			frameConstant.setId(new Integer(constantId_modify));
			frameConstantService.findFrameConstant(ri, frameConstant);
			frameConstant = (FrameConstantPO)ri.getObj();
			RequestUtil.setAttribute(request, "frameConstant_modify", frameConstant);
		}
		
		RequestUtil.setAttribute(request, "constantList", constantList);
		RequestUtil.setAttribute(request, "constantKey", constantKey);
		RequestUtil.setAttribute(request, "constantName_find", constantName_find);
		return BaseAction.MODIFY;
	}
	
	public String modifyExc() throws BasicException {
		if (frameConstant.getId() != null) {
			frameConstantService.updateFrameConstant(ri, frameConstant);
		} else {
			String constantKey = RequestUtil.getParameterString(request, "constantKey");
			frameConstant.setModuleName(constantKey);
			frameConstantService.saveFrameConstant(ri, frameConstant);
		}
		return BaseAction.MODIFY_EXC;
	}
	
	public String delete() throws BasicException {
		if (frameConstant.getId() != null) {
			frameConstantService.deleteFrameConstant(ri, frameConstant.getId());
		}
		return BaseAction.DELETE;
	}

	public FrameConstantService getFrameConstantService() {
		return frameConstantService;
	}
	public void setFrameConstantService(FrameConstantService frameConstantService) {
		this.frameConstantService = frameConstantService;
	}
	public FrameConstantPO getFrameConstant() {
		return frameConstant;
	}
	public void setFrameConstant(FrameConstantPO frameConstant) {
		this.frameConstant = frameConstant;
	}

}
